package com.hnust.service.impl;


import com.hnust.dao.TeacherClassDao;
import com.hnust.domain.Class;
import com.hnust.domain.Course;
import com.hnust.domain.Student;
import com.hnust.domain.record.*;
import com.hnust.service.TeacherStuService;
import com.hnust.util.JwtToken;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 学生管理操作
 * @author：xinrui
 * @date：2020/10/30
 */
@Service("teacherStuService")
public class TeacherStuServiceImpl implements TeacherStuService {
    @Autowired
    private TeacherClassDao teacherClassDao;

    //    获取老师所有课程题目的正确率
    @Override
    public List<Visual2> getVisual2(String tea_id, String token){

        List<Visual2> visual2s = new ArrayList<>();
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            Visual2 visual2 = new Visual2();
            visual2.setFalseTotal(55);
            visual2s.add(visual2);
            return visual2s;
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");
        String tea_number = teacherClassDao.queryTeaNumberByTeaId(tea_id);
        if(tea_number==null){
//            Visual2 visual2 = new Visual2();
//            visual2.setFalseTotal(56);
//            visual2s.add(visual2);
            return visual2s;
        }

        List<Class> classes = teacherClassDao.queryClassByTeaId(tea_id);
        List<Student> students = new ArrayList<>();

        try {
            for(Class classItem :classes){
                students = teacherClassDao.queryStudentsByClassId(classItem.getClaId());
            }
        }catch (Exception e) {
            Visual2 visual2 = new Visual2();
            visual2.setFalseTotal(57);
            visual2s.add(visual2);
            return visual2s;
        }
        List<Course> courses = teacherClassDao.queryCourseByTeaId(tea_id);
        try {
            Integer sum=0;
            Integer right =0;
            for(Course course : courses){
                Visual2 visual2 = new Visual2();
                try {
                    for (Student student:students){
                        sum = sum +teacherClassDao.querySumCourse(course.getCouId(),student.getStuId());
                        right = right +teacherClassDao.queryRightCourse(course.getCouId(),student.getStuId());
                    }
                }catch (Exception e ){
                    Visual2 visual21 = new Visual2();
                    visual21.setFalseTotal(70);
                    visual2s.add(visual21);
                    return visual2s;
                }
                Integer falsesum = sum-right;
                visual2.setCourseName(course.getName());
                visual2.setRightTotal(right);
                visual2.setFalseTotal(falsesum);
                visual2s.add(visual2);
            }
        }catch (Exception e){
            Visual2 visual2 = new Visual2();
            visual2.setFalseTotal(58);
            visual2s.add(visual2);
            return visual2s;
        }
        return visual2s;
    }
    //    获取各个班级的做题信息
    @Override
    public List<Visual1> getVisual1(String tea_id,String token){
        List<Visual1> visual1s = new ArrayList<>();
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
//            无效令牌
            Visual1 visual1 = new Visual1();
            visual1.setSubjectCount(88);
            visual1s.add(visual1);
            return visual1s;
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");
        String tea_number = teacherClassDao.queryTeaNumberByTeaId(tea_id);
//        根据老师Id 获取到教工号，
        if(tea_number==null){
            Visual1 visual1 = new Visual1();
            visual1.setSubjectCount(89);
            visual1s.add(visual1);
            return visual1s;
        }
        List<Class> classes = teacherClassDao.queryClassByTeaId(tea_id);
//        获取到班级的各个id
        for(Class clas:classes){
//            遍历每个班级的情况
            List<Course> courses =teacherClassDao.queryAllCourseByCourseGroupId(clas.getClaId());
//            获取到各个班级的各个课程
            List<Student> students = teacherClassDao.queryStudentsByClassId(clas.getClaId());
//            获取到每个班级的学生信息
            try {
                for (Course course : courses) {
                    Visual1 visual1 = new Visual1();
                    Integer total = 0;
                    try {
                        for (Student student : students) {
                            total = total + teacherClassDao.querySumCourse(course.getCouId(), student.getStuId());
                        }
                    } catch (Exception e){
                        Visual1 visual11 = new Visual1();
                        visual11.setSubjectCount(92);
                        visual1s.add(visual11);
                        return visual1s;
                    }
                    String majorName = teacherClassDao.queryMajorByClassId(clas.getClaId());
                    visual1.setClassName(clas.getGrade()+"级"+majorName+"专业"+clas.getClaNumber()+"班");
                    visual1.setCourseName(course.getName());
                    visual1.setSubjectCount(total);
                    visual1s.add(visual1);
                }
            }catch (Exception e){
                Visual1 visual1 = new Visual1();
                visual1.setSubjectCount(90);
                visual1s.add(visual1);
                return visual1s;
            }
        }
        return  visual1s;
    }


    @Override
    public StudentDetailAndTotal getCourseInfo(String id, String token, String stu_name,String class_name,String course_name,Integer now_page){
//       token存储用户id和密码
        String msg ="学生信息查看成功";
        StudentDetailAndTotal studentDetailAndTotal=new StudentDetailAndTotal();
        List<Student> students =new ArrayList<>();
        List<Course> courses = new ArrayList<>();
        List<StudentDetailSubjectData> studentDetailSubjectData = teacherClassDao.searchTeacherStu(course_name,stu_name,class_name,id);
        List<StudentDetailSubjectData> studentDetailSubjectDataFinal = new ArrayList<>();
        List<StudentDetailSubjectData> studentDetailSubjectDatalinshi = new ArrayList<>();
        Map<String, String> map = null;
        try {
            map = JwtToken.verifyToken(token);
        } catch (Exception e) {
            StudentDetailSubjectData visual2 = new StudentDetailSubjectData();
            visual2.setPercent(0.55);
            studentDetailSubjectDataFinal.add(visual2);
            studentDetailAndTotal.setTotal(studentDetailSubjectDataFinal.size());
            studentDetailAndTotal.setList(studentDetailSubjectDataFinal);
            return studentDetailAndTotal;
        }
        String idInToken = map.get("id");
        String passwordInToken = map.get("password");
        String tea_number = teacherClassDao.queryTeaNumberByTeaId(id);
        if(tea_number==null){
            StudentDetailSubjectData visual2 = new StudentDetailSubjectData();
            visual2.setPercent(0.55);
            studentDetailSubjectDataFinal.add(visual2);
            studentDetailAndTotal.setTotal(studentDetailSubjectDataFinal.size());
            studentDetailAndTotal.setList(studentDetailSubjectDataFinal);
            return studentDetailAndTotal;
        }

        try {
            for(StudentDetailSubjectData studentDetailSubjectData1:studentDetailSubjectData){
                Student student = new Student();
                Course course = new Course();
                String students1 = studentDetailSubjectData1.getId();
                student.setStuNumber(students1);
                students.add(student);

                String course1 =studentDetailSubjectData1.getCourse();
                course.setName(course1);
                courses.add(course);
            }
        }catch (Exception e){
            StudentDetailSubjectData subjectData = new StudentDetailSubjectData();
            subjectData.setId("90");
            studentDetailSubjectDataFinal.add(subjectData);
            studentDetailAndTotal.setTotal(studentDetailSubjectDataFinal.size());
            studentDetailAndTotal.setList(studentDetailSubjectDataFinal);
            return studentDetailAndTotal;
        }
        try {
            for(Student student:students){
                for (Course course:courses) {
                    try {
                        for (StudentDetailSubjectData s : studentDetailSubjectData) {
                            if (student.getStuNumber() == s.getId() && course.getName() == s.getCourse()) {
                                StudentDetailSubjectData detailSubjectData = new StudentDetailSubjectData();
                                Integer total = teacherClassDao.searchSumCourse(course.getName(), student.getStuNumber());
                                Integer right = teacherClassDao.searchRightCourse(course.getName(), student.getStuNumber());
                                Double percent = (double)right/ (double)total;

                                detailSubjectData.setTotal(total);
                                detailSubjectData.setRight(right);
                                detailSubjectData.setId(s.getId());
                                detailSubjectData.setClasses(s.getClasses());
                                detailSubjectData.setCourse(s.getCourse());
                                detailSubjectData.setName(s.getName());
                                detailSubjectData.setProfession(s.getProfession());
                                if(total==0)
                                {
                                    detailSubjectData.setPercent(0.0);
                                }
                                else {
                                    detailSubjectData.setPercent(percent*100);
                                }

                                studentDetailSubjectDatalinshi.add(detailSubjectData);
                                break;
                            }
                        }
                    }catch (Exception e){
                        StudentDetailSubjectData subjectData = new StudentDetailSubjectData();
                        subjectData.setId("91");
                        studentDetailSubjectDataFinal.add(subjectData);
                        studentDetailAndTotal.setTotal(studentDetailSubjectDataFinal.size());
                        studentDetailAndTotal.setList(studentDetailSubjectDataFinal);
                        return studentDetailAndTotal;
                    }
                }
            }
        } catch (Exception e){
            StudentDetailSubjectData subjectData = new StudentDetailSubjectData();
            subjectData.setId("92");
            studentDetailSubjectDataFinal.add(subjectData);
            studentDetailAndTotal.setTotal(studentDetailSubjectDataFinal.size());
            studentDetailAndTotal.setList(studentDetailSubjectDataFinal);
            return studentDetailAndTotal;
        }

        int page = studentDetailSubjectDatalinshi.size()/13+1;
        int now = (studentDetailSubjectDatalinshi.size())%13;
        studentDetailAndTotal.setTotal(studentDetailSubjectDatalinshi.size());
        if(page == now_page){
            studentDetailSubjectDataFinal = studentDetailSubjectDatalinshi.subList((now_page-1)*13,(now_page-1) *13+now);
        }
        else {
            studentDetailSubjectDataFinal = studentDetailSubjectDatalinshi.subList((now_page - 1) * 13, now_page *13);
        }
//        return studentDetailSubjectDatalinshi;

        studentDetailAndTotal.setList(studentDetailSubjectDataFinal);
        return studentDetailAndTotal;

    }
}
